# -*- coding: utf-8 -*-
import sys,androidTools,Const,Util,json

def getAChannelApk(name_apk_base,channel_id ,path_apk_channel):
	'''
	修改包渠道号
	'''
	#对渠道号文件进行修改
	Util.changeChannel(Const.PATH_APK_FILE_SOURCEID, channel_id)

	#直接在APK文件中修改渠道号
	androidTools.zipAddFile(name_apk_base,Const.PATH_APK_FILE_SOURCEID)

	#对未签名的APK包，进行签名
	androidTools.SignApk(Const.BASE_NAME_APKUNALIGN,name_apk_base,Const.PATH_KEYSTORE,Const.NAME_KEYSTORE,Const.SIGN_KEYPASSWD)
	androidTools.zipalign(Const.BASE_NAME_APKUNALIGN,path_apk_channel)

if __name__ == '__main__':
	argLen = len(sys.argv)
	if(argLen < 5):
		print ('参数个数错误！')
		exit(1)
	VERSION = sys.argv[1]
	DIR_APK_BASE = sys.argv[2]
	DIR_APK_CHANNEL = sys.argv[3]
	CHANNELS = sys.argv[4]
        print ("打印传递过来的channels")
        packChannels = CHANNELS.split(',')
        channelString = CHANNELS        
        for channelName in packChannels:
            print channelName + " * "
        CHANNELS = '{"channels":[{"code":"3033a","name":"tengxunguanjia"},{"code":"3032a","name":"yunyinghuodong"},{"code":"jfq","name":"jfq"},{"code":"3001a","name":"360"},{"code":"3002a","name":"baidu"},{"code":"3003a","name":"91"},{"code":"3004a","name":"hiapkmarket"},{"code":"3005a","name":"qq"},{"code":"3006a","name":"wdj"},{"code":"3007a","name":"goapk"},{"code":"3008a","name":"appchinamarket"},{"code":"3009a","name":"mumayi"},{"code":"3010a","name":"gfan"},{"code":"3011a","name":"netease"},{"code":"3012a","name":"eoemarket"},{"code":"3013a","name":"nduoa"},{"code":"3014a","name":"ppuc"},{"code":"3015a","name":"oppo"},{"code":"3016a","name":"huaweidevice"},{"code":"3017a","name":"jinli"},{"code":"3018a","name":"ztems"},{"code":"3019a","name":"lenovo"},{"code":"3020a","name":"samsung"},{"code":"3021a","name":"xiaomi"},{"code":"3022a","name":"meizu"},{"code":"3023a","name":"10086mm"},{"code":"3024a","name":"wostore"},{"code":"3025a","name":"189storepublic"},{"code":"3026a","name":"google"},{"code":"appstore","name":"appstore"},{"code":"2001a","name":"guanwang"},{"code":"2002a","name":"weibo"},{"code":"10001a","name":"weiboqiangqian"},{"code":"10002a","name":"weixinqiangqian"},{"code":"10003a","name":"qiangqianyaoqing"},{"code":"jdb","name":"jdbshengji"},{"code":"3027a","name":"weiboyingyong"},{"code":"2003a","name":"weixin"},{"code":"10004a","name":"pengyouH5"},{"code":"10005a","name":"jiyinH5"},{"code":"10006a","name":"xinshouH5"},{"code":"10007a","name":"qiangqianditui1"},{"code":"10008a","name":"qiangqianditui2"},{"code":"10009a","name":"qiangqianditui3"},{"code":"10010a","name":"qiangqianditui4"},{"code":"10011a","name":"qiangqianditui5"},{"code":"10012a","name":"qiangqianditui6"},{"code":"10013a","name":"qiangqianditui7"},{"code":"10014a","name":"qiangqianditui8"},{"code":"10015a","name":"qiangqianditui9"},{"code":"10016a","name":"qiangqianditui10"},{"code":"3028a","name":"sinayingyong"},{"code":"3029a","name":"baiduSEM"},{"code":"3030a","name":"UCSEM"},{"code":"3031a","name":"sougouSEM"}]}'
	json_cannels = json.loads(CHANNELS)
	cannel_list = json_cannels['channels']

	name_apk_base = Const.NAME_FILE_PREFIX + VERSION + '.apk'
	path_apk_base = DIR_APK_BASE + '/' + name_apk_base

	dir_apk_channel = DIR_APK_CHANNEL+'/'+ VERSION +'/'

	#创建该版本对应的渠道包文件夹（版本名去新建文件夹名）
	Util.makedirs(dir_apk_channel) 

	#先将母包拷贝出来
        print ("Util.copyFile path:")
        print path_apk_base
	Util.copyFile(path_apk_base,'./')

	#生成每个渠道包
	for cannel_item in cannel_list:
		channel_code = cannel_item['code']
		channel_name = cannel_item['name']
                nPos = channelString.find(channel_name)
                if(nPos>=0):
	        	path_apk_channel = dir_apk_channel+Const.NAME_FILE_PREFIX+channel_name+'_'+VERSION+'_'+channel_code+'.apk'
	        	getAChannelApk(name_apk_base,channel_code,path_apk_channel)

	#清理操作
	#Util.DelFolder(Const.BASE_NAME_APKDIR)
	Util.DelFile(name_apk_base)
	Util.DelFile(Const.BASE_NAME_APKUNALIGN)
